package com.laiyizhan.cms.first.service.impl;

import com.laiyizhan.cms.first.request.ThirdAndFourthRequest;
import com.laiyizhan.cms.first.service.IThirdAndFourthService;
import com.laiyizhan.common.mapper.*;
import com.laiyizhan.common.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static org.springframework.beans.BeanUtils.copyProperties;

/**
 * Created by SoulWolf on 2017/7/20.
 */
@Service
public class ThirdAndFourthServiceImpl implements IThirdAndFourthService {

    @Autowired
    Third_messageMapper third_messageMapper;
    @Autowired
    Third_aMapper third_aMapper;
    @Autowired
    Third_bMapper third_bMapper;
    @Autowired
    Fourth_messageMapper fourth_messageMapper;
    @Autowired
    Fourth_aMapper fourth_aMapper;
    @Autowired
    Fourth_bMapper fourth_bMapper;
    @Autowired
    StateMapper stateMapper;

    public State ThirdAndFourth_index(ThirdAndFourthRequest request) {
        State temp = stateMapper.selectByName(request.getoName());
        if (temp == null) {
            State state = new State();
            state.setHospital(request.getoName());
            state.setA("0");
            state.setB("0");
            state.setC("0");
            state.setD("0");
            state.setE("0");
            state.setF("0");
            state.setG("0");
            stateMapper.insertSelective(state);
            return state;
        }else {
            return stateMapper.selectByName(request.getoName());
        }
    }




    private String[] t_a_project = {"村医人数","服务人口数","日均门诊人次数","年门诊总收入",
    "次均门诊费用","其中：药品收入所占比重","全部补助收入","其中：基本公共卫生服务补助",
    "基本药物补助","一般诊疗费补助","其他补助","乡村集体补助","开展公共卫生服务占全部工作比重"};
    private String[] t_a_unit={"人","人","人次","万元","元","%","元/年","元/年","元/年","元/年","元/年","元/年","%"};

    private String[] f_a_project = {"是否为医疗保险定点机构","人员数","日均门诊人次数","年门诊总收入",
            "次均门诊费用","其中：药品收入所占比重"};
    private String[] f_a_unit={"","人","人次","万元","元","%"};

    public String Third_a(ThirdAndFourthRequest request){
        Check_message("a",request.getoName());
        /*插入个人信息*/
        Third_message third_message = new Third_message();
        copyProperties
                (request,third_message);
        third_message.setHospital(request.getoName());
        third_messageMapper.insertSelective(third_message);

        /*数据处理*/
        List<Third_a> list = new ArrayList<>();
        for(int i =0;i<26;i=i+2){
            Third_a third_a = new Third_a();
            third_a.setHospital(request.getoName());
            third_a.setProject(t_a_project[i/2]);
            third_a.setUnit(t_a_unit[i/2]);
            if(request.getThirdA()[i]!=""){
                third_a.setCount(request.getThirdA()[i]);
            }else{
                third_a.setCount("0");
            }
            third_a.setRemark(request.getThirdA()[i+1]);
            list.add(third_a);
        }
        third_aMapper.insertBatch(list);
        Change_state("a",request.getoName());
        return "ok";


    }
    public String Third_b(ThirdAndFourthRequest request){
        Check_message("b",request.getoName());
        List<Third_b> list = new ArrayList<>();
        for(int i=0;i<240;i=i+6){
            Third_b third_b = new Third_b();
            third_b.setHospital(request.getoName());
            third_b.setAge(request.getThirdB_p()[i]);
            third_b.setSex(request.getThirdB_p()[i+1]);
            third_b.setDiagnosis(request.getThirdB_p()[i+2]);
            third_b.setCountOne(request.getThirdB_p()[i+3]);
            third_b.setCountTwo(request.getThirdB_p()[i+4]);
            third_b.setCountThree(request.getThirdB_p()[i+5]);
            list.add(third_b);
        }
        for(int i=0;i<240;i=i+6){
            Third_b third_b = new Third_b();
            third_b.setHospital(request.getoName());
            third_b.setAge(request.getThirdB_pp()[i]);
            third_b.setSex(request.getThirdB_pp()[i+1]);
            third_b.setDiagnosis(request.getThirdB_pp()[i+2]);
            third_b.setCountOne(request.getThirdB_pp()[i+3]);
            third_b.setCountTwo(request.getThirdB_pp()[i+4]);
            third_b.setCountThree(request.getThirdB_pp()[i+5]);
            list.add(third_b);
        }
        for(int i=0;i<240;i=i+6){
            Third_b third_b = new Third_b();
            third_b.setHospital(request.getoName());
            third_b.setAge(request.getThirdB_ppp()[i]);
            third_b.setSex(request.getThirdB_ppp()[i+1]);
            third_b.setDiagnosis(request.getThirdB_ppp()[i+2]);
            third_b.setCountOne(request.getThirdB_ppp()[i+3]);
            third_b.setCountTwo(request.getThirdB_ppp()[i+4]);
            third_b.setCountThree(request.getThirdB_ppp()[i+5]);
            list.add(third_b);
        }
        for(int i=0;i<180;i=i+6){
            Third_b third_b = new Third_b();
            third_b.setHospital(request.getoName());
            third_b.setAge(request.getThirdB_pppp()[i]);
            third_b.setSex(request.getThirdB_pppp()[i+1]);
            third_b.setDiagnosis(request.getThirdB_pppp()[i+2]);
            third_b.setCountOne(request.getThirdB_pppp()[i+3]);
            third_b.setCountTwo(request.getThirdB_pppp()[i+4]);
            third_b.setCountThree(request.getThirdB_pppp()[i+5]);
            list.add(third_b);
        }
        third_bMapper.insertBatch(list);
        Change_state("b",request.getoName());
        return "ok";


    }

    public String Fourth_a(ThirdAndFourthRequest request){
        Check_message("c",request.getoName());
        /*插入个人信息*/
        Fourth_message fourth_message = new Fourth_message();
        copyProperties(request,fourth_message);
        fourth_message.setHospital(request.getoName());
        fourth_messageMapper.insertSelective(fourth_message);

        /*数据处理*/
        List<Fourth_a> list = new ArrayList<>();
        for(int i =0;i<12;i=i+2){
            Fourth_a fourth_a = new Fourth_a();
            fourth_a.setHospital(request.getoName());
            fourth_a.setProject(f_a_project[i/2]);
            fourth_a.setUnit(f_a_unit[i/2]);
            if(request.getFourthA()[i]!=""){
                fourth_a.setCount(request.getFourthA()[i]);
            }else{
                fourth_a.setCount("0");
            }
            fourth_a.setRemark(request.getFourthA()[i+1]);
            list.add(fourth_a);
        }
        fourth_aMapper.insertBatch(list);
        Change_state("a",request.getoName());
        return "ok";


    }


    public String Fourth_b(ThirdAndFourthRequest request){
        Check_message("d",request.getoName());
        List<Fourth_b> list = new ArrayList<>();
        for(int i=0;i<240;i=i+6){
            Fourth_b fourth_b = new Fourth_b();
            fourth_b.setHospital(request.getoName());
            fourth_b.setAge(request.getFourthB_p()[i]);
            fourth_b.setSex(request.getFourthB_p()[i+1]);
            fourth_b.setDiagnosis(request.getFourthB_p()[i+2]);
            fourth_b.setCountOne(request.getFourthB_p()[i+3]);
            fourth_b.setCountTwo(request.getFourthB_p()[i+4]);
            fourth_b.setCountThree(request.getFourthB_p()[i+5]);
            list.add(fourth_b);
        }
        for(int i=0;i<240;i=i+6){
            Fourth_b fourth_b = new Fourth_b();
            fourth_b.setHospital(request.getoName());
            fourth_b.setAge(request.getFourthB_pp()[i]);
            fourth_b.setSex(request.getFourthB_pp()[i+1]);
            fourth_b.setDiagnosis(request.getFourthB_pp()[i+2]);
            fourth_b.setCountOne(request.getFourthB_pp()[i+3]);
            fourth_b.setCountTwo(request.getFourthB_pp()[i+4]);
            fourth_b.setCountThree(request.getFourthB_pp()[i+5]);
            list.add(fourth_b);
        }
        for(int i=0;i<240;i=i+6){
            Fourth_b fourth_b = new Fourth_b();
            fourth_b.setHospital(request.getoName());
            fourth_b.setAge(request.getFourthB_ppp()[i]);
            fourth_b.setSex(request.getFourthB_ppp()[i+1]);
            fourth_b.setDiagnosis(request.getFourthB_ppp()[i+2]);
            fourth_b.setCountOne(request.getFourthB_ppp()[i+3]);
            fourth_b.setCountTwo(request.getFourthB_ppp()[i+4]);
            fourth_b.setCountThree(request.getFourthB_ppp()[i+5]);
            list.add(fourth_b);
        }
        for(int i=0;i<180;i=i+6){
            Fourth_b fourth_b = new Fourth_b();
            fourth_b.setHospital(request.getoName());
            fourth_b.setAge(request.getFourthB_pppp()[i]);
            fourth_b.setSex(request.getFourthB_pppp()[i+1]);
            fourth_b.setDiagnosis(request.getFourthB_pppp()[i+2]);
            fourth_b.setCountOne(request.getFourthB_pppp()[i+3]);
            fourth_b.setCountTwo(request.getFourthB_pppp()[i+4]);
            fourth_b.setCountThree(request.getFourthB_pppp()[i+5]);
            list.add(fourth_b);
        }
        fourth_bMapper.insertBatch(list);
        Change_state("b",request.getoName());
        return "ok";


    }
    public String Check_message(String table,String oName){
        switch (table)
        {
            case "a":
                if(third_aMapper.selectByName(oName)!=null){
                    third_aMapper.deleteByName(oName);
                }
                break;
            case "b":
                if(third_bMapper.selectByName(oName)!=null){
                    third_bMapper.deleteByName(oName);
                }
                break;
            case "c":
                if(fourth_aMapper.selectByName(oName)!=null){
                    fourth_aMapper.deleteByName(oName);
                }
                break;
            case "d":
                if(fourth_bMapper.selectByName(oName)!=null){
                    fourth_bMapper.deleteByName(oName);
                }
                break;
        }

        return "ok";
    }

    private String Change_state(String table,String oName) {
        State state = stateMapper.selectByName(oName);
        switch (table)
        {
            case "a":
                state.setA("1");
                break;
            case "b":
                state.setB("1");
                break;
        }
        stateMapper.updateByName(state);
        return "ok";
    }

}
